package com.ex.back.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ex.back.sys.entity.User;
import com.ex.back.sys.service.IUserService;
import commom.vo.result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author noob
 * @since 2023-04-06
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/all")
    public result<List<User>> getAllUser(){
        List<User> list = userService.list();
        return result.success(list,"查询成功");
    }

//    @PostMapping("/login")
//    public result<Map<String,Object>> login(@RequestBody User user){
//        Map<String,Object> data = userService.login(user);
//        if(data != null){
//            return result.success(data);
//        }
//        return result.fail(20002,"用户名或密码错误");
//    }
    @PostMapping("/login")
    public result<Map<String,Object>> login(@RequestBody User user){
        Map<String,Object> data = userService.login(user);
        if(data != null){
            return result.success(data);
        }
        return result.fail(20002,"用户名或密码错误");
    }

    @GetMapping("/info")
    public result<Map<String,Object>> getUserInfo(@RequestParam("token") String token){
        // 根据token获取用户信息，redis
        Map<String,Object> data = userService.getUserInfo(token);
        if(data != null){
            return result.success(data);
        }
        return result.fail(20003,"登录信息无效，请重新登录");
    }
    @PostMapping("/logout")
    public result<?> logout(@RequestHeader("X-Token") String token){
        userService.logout(token);
        return result.success();
    }

}
